import 'package:flutter/material.dart';

///
/// 这个组件用来重新加载整个 child Widget 的。
/// 当我们需要重启 App 的时候，可以使用这个方案。
/// https://stackoverflow.com/questions/50115311/flutter-how-to-force-an-application-restart-in-production-mode
///
class RestartApp extends StatefulWidget {
  final Widget child;

  const RestartApp({Key? key, required this.child})
      : super(key: key);

  static restartApp(BuildContext context) {
    final _RestartAppState? state =
    context.findAncestorStateOfType<_RestartAppState>();
    state!.restartApp();
  }

  @override
  _RestartAppState createState() => _RestartAppState();
}

class _RestartAppState extends State<RestartApp> {
  Key key = UniqueKey();

  void restartApp() {
    setState(() {
      key = UniqueKey();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      key: key,
      child: widget.child,
    );
  }
}